package lk.icta.common.web.action;

import com.opensymphony.xwork2.ActionSupport;
import lk.icta.common.AppConstants;
import lk.icta.common.entity.Branch;
import lk.icta.common.entity.DSDivision;
import lk.icta.common.entity.District;
import lk.icta.common.web.WebConstants;
import lk.icta.user.common.Constant;
import lk.icta.user.entity.User;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * @author Mahesha Kalpanie
 */
public class BaseAction extends ActionSupport implements SessionAware {

    private static final Logger logger = LoggerFactory.getLogger(BaseAction.class);

    protected static final String WARNING = "warning";
    protected Map session;
    protected User user;
    private String userDistrictName;
    private String userBranchName;
    protected String language;
    protected int userProvinceId;
    protected int userDistrictId;
    protected int userBranchId;

    public void setSession(Map session) {
        try {
            this.session = session;
            Locale locale = (Locale) session.get(WebConstants.SESSION_USER_LANG);

            logger.debug("setting language before : {}", language);

            if (locale != null) {
                language = locale.getLanguage();
                logger.debug("setting language : {}", language);
            } else {
                locale = new Locale(AppConstants.ENGLISH, AppConstants.COUNTRY);
                session.put(WebConstants.SESSION_USER_LANG, locale);
                language = locale.getLanguage();
            }

            user = (User) session.get(AppConstants.SESSION_USER_BEAN);
            if (user != null) {
                logger.debug("set Session user {}", user.getUsername());
                findLocation();
                logger.debug("userDistrictName {} / userBranchName {}", userDistrictName, userBranchName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*protected void findProvince() {
        Set<DSDivision> dsDivisions = user.getDsDivisions();
        if (dsDivisions != null && dsDivisions.size() > 0 && !user.getRole().getId().equals(AppConstants.ORIP_USER_ROLE)) {
            District district = dsDivisions.iterator().next().getDistrict();

            userProvinceId = district.getProvince().getId();
            userDistrictId = district.getId();
            userDsId = dsDivisions.iterator().next().getId();

            if (language.equals(AppConstants.ENGLISH)) {
                userProvinceName = district.getProvince().getEnProvinceName() + " Province";
            } else if (language.equals(AppConstants.SINHALA)) {
                userProvinceName = district.getProvince().getSiProvinceName() + " පළාත";
            } else if (language.equals(AppConstants.TAMIL)) {
                userProvinceName = district.getProvince().getTaProvinceName();
            }
        } else if (user.getRole().getId().equals(AppConstants.ORIP_USER_ROLE)) {
            if (language.equals(AppConstants.ENGLISH)) {
                userProvinceName = " Head Office";
            } else if (language.equals(AppConstants.SINHALA)) {
                userProvinceName = " Head Office";
            } else if (language.equals(AppConstants.TAMIL)) {
                userProvinceName = "Head Office";
            }
        }
    }*/

    protected void findLocation() {
        Set<District> districts = user.getDistricts();
        if (districts != null && districts.size() > 0) {
            District district = districts.iterator().next();

            userProvinceId = district.getProvince().getId();
            userDistrictId = district.getId();
            if (user.getBranch() != null) {
                Branch branch = user.getBranch();
                userBranchId = branch.getId();
                if (branch != null) {
                    if (language.equals(AppConstants.ENGLISH)) {
                        userBranchName = branch.getEnBranchName();
                    } else if (language.equals(AppConstants.SINHALA)) {
                        userBranchName = branch.getSiBranchName();
                    } else if (language.equals(AppConstants.TAMIL)) {
                        userBranchName = branch.getTaBranchName();
                    }
                }
            }

            if (language.equals(AppConstants.ENGLISH)) {
                userDistrictName = district.getEnDistrictName();
            } else if (language.equals(AppConstants.SINHALA)) {
                userDistrictName = district.getSiDistrictName();
            } else if (language.equals(AppConstants.TAMIL)) {
                userDistrictName = district.getTaDistrictName();
            }
        } else if (user.getBranch() != null) {
            Branch branch = user.getBranch();
            userBranchId = branch.getId();
            if (branch != null) {
                if (language.equals(AppConstants.ENGLISH)) {
                    userBranchName = branch.getEnBranchName();
                } else if (language.equals(AppConstants.SINHALA)) {
                    userBranchName = branch.getSiBranchName();
                } else if (language.equals(AppConstants.TAMIL)) {
                    userBranchName = branch.getTaBranchName();
                }
            }

        } else {
            if (language.equals(AppConstants.ENGLISH)) {
                userDistrictName = " Head Office";
            } else if (language.equals(AppConstants.SINHALA)) {
                userDistrictName = " Head Office";
            } else if (language.equals(AppConstants.TAMIL)) {
                userDistrictName = "Head Office";
            }
        }
    }

    public User getUser() {
        return user;
    }

    public String getLanguage() {
        return language;
    }

    public int getUserProvinceId() {
        return userProvinceId;
    }

    public int getUserDistrictId() {
        return userDistrictId;
    }

    public String getUserDistrictName() {
        return userDistrictName;
    }

    public void setUserDistrictName(String userDistrictName) {
        this.userDistrictName = userDistrictName;
    }

    public String getUserBranchName() {
        return userBranchName;
    }

    public void setUserBranchName(String userBranchName) {
        this.userBranchName = userBranchName;
    }
}
